#include "stm32f10x.h"                  // Device header
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "misc.h"
#include "voice.h"
#include "Face_Config.h"

void BlueTooth_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);

	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	USART_InitTypeDef UASRT_InitStructure;
	UASRT_InitStructure.USART_BaudRate=9600;
	UASRT_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	UASRT_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
	UASRT_InitStructure.USART_Parity=USART_Parity_No;
	UASRT_InitStructure.USART_StopBits=USART_StopBits_1;
	UASRT_InitStructure.USART_WordLength=USART_WordLength_8b;
	USART_Init(USART3,&UASRT_InitStructure);
	
	USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//蓝牙接收中断配置，也就是如果接送到消息就直接中断
	
	//中断
	// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//分组2
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel=USART3_IRQn;//特定的通道
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART3,ENABLE);
}

void USART3_IRQHandler(void)
{
	if(USART_GetITStatus(USART3,USART_IT_RXNE)==SET)//如果接受到
	{
        if (USART_ReceiveData(USART3) == 0xA1) { //立正 
            Face_Mode = enum_attention;
            Face_Config();
            Action_Mode = enum_attention;
        }
        if (USART_ReceiveData(USART3) == 0xA2) { //叫 
            Face_Mode = enum_cry;
            Face_Config();
            Action_Mode = enum_cry;
        }
        if (USART_ReceiveData(USART3) == 0xA3) { //睡觉 
            Face_Mode = enum_sleep;
            Face_Config();
            Action_Mode = enum_sleep;
        }
        if (USART_ReceiveData(USART3) == 0xA4) { //趴下 
            Face_Mode = enum_fall;
            Face_Config();
            Action_Mode = enum_fall;
        }
        if (USART_ReceiveData(USART3) == 0xA5) { //蹲下 
            Face_Mode = enum_squat;
            Face_Config();
            Action_Mode = enum_squat;
        }
        if (USART_ReceiveData(USART3) == 0xA6) { //摇尾巴 
            (WeiBa == 0) ? (WeiBa = 1) : (WeiBa = 0);
            Face_Mode = enum_wag_tail;
            Face_Config();
            Action_Mode = enum_wag_tail;
        }
        if (USART_ReceiveData(USART3) == 0xA7) { //摇摆 
            Face_Mode = enum_swing;
            Face_Config();
            Action_Mode = enum_swing;
        }
        if (USART_ReceiveData(USART3) == 0xA8) { //握手 
            Face_Mode = enum_handshake;
            Face_Config();
            Action_Mode = enum_handshake;
        }
        if (USART_ReceiveData(USART3) == 0xA9) { //坐下 
            Face_Mode = enum_sit;
            Face_Config();
            Action_Mode = enum_sit;
        }
        if (USART_ReceiveData(USART3) == 0xB1) { //前进 
            Face_Mode = enum_forward;
            Face_Config();
            Action_Mode = enum_forward;
        }
        if (USART_ReceiveData(USART3) == 0xB2) { //后退 
            Face_Mode = enum_retreat;
            Face_Config();
            Action_Mode = enum_retreat;
        }
        if (USART_ReceiveData(USART3) == 0xB3) { //左转 
            Face_Mode = enum_turn_left;
            Face_Config();
            Action_Mode = enum_turn_left;
        }
        if (USART_ReceiveData(USART3) == 0xB4) { //右转 
            Face_Mode = enum_turn_right;
            Face_Config();
            Action_Mode = enum_turn_right;
        }
        if (USART_ReceiveData(USART3) == 0xB5) { //向前跳 
            Face_Mode = enum_jump_up;
            Face_Config();
            Action_Mode = enum_jump_up;
        }
        if (USART_ReceiveData(USART3) == 0xB6) { //向后跳 
            Face_Mode = enum_jump_down;
            Face_Config();
            Action_Mode = enum_jump_down;
        }
        USART_ClearITPendingBit(USART3,USART_IT_RXNE);
    }

}
